﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_GoodsAdd : System.Web.UI.Page
{
    Int64 ProductId;
    public string strSql;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {

            if (!IsPostBack)
            {
                if (Request.QueryString["ProductId"] == null)
                {
                    Response.Write("没有要添加的商品编码！！！");
                }
                else
                {

                    ProductId = Convert.ToInt64(SysFun.IsSQL(Request.QueryString["ProductId"].ToString()));
                    Db db = new Db();
                    System.Data.IDataReader dr;
                    strSql = "select * from Product where ProductId=" + ProductId.ToString();
                    dr = db.DataReader(strSql);
                    while (dr.Read())
                    {
                        labProductCode.Text = dr["ProductCode"].ToString();
                        hidProductId.Value = ProductId.ToString();
                        labProductName.Text = dr["productname"].ToString();
                        //string _strMedicinemodel = dr["medicinemodel"].ToString();
                        //string _strOutlookc = dr["outlookc"].ToString();
                        //labMedicinemodel.Text = _strMedicinemodel;
                        //if (_strMedicinemodel == "大输液" && _strOutlookc.IndexOf("玻瓶") == null)
                        //{
                        //    labOutlookc.Text = _strOutlookc + "大容量制剂（治疗用大输液）未标明包材的为玻瓶。";

                        //}
                        //else
                        //{
                        //    labOutlookc.Text = _strOutlookc;
                        //}

                        labOutlookc.Text = dr["outlookc"].ToString();
                        labMedicinemodel.Text = dr["medicinemodel"].ToString();
                        labUnit.Text = dr["unit"].ToString();
                        txtFactor.Text = dr["xishu"].ToString();
                        txtPackUnit.Text = dr["unit"].ToString();
                    }
                    dr.Close();
                    db.Close();
                    dr = null;
                    db = null;
                    txtPackUnit.Visible = false;
                    //chkIsImport.Checked =true  ;


                }
            }
        }

    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        Int64 IsSalerId, IsSalerId_dl = 0, AccountId_TJR;
        HZ.MBSM.Model.Goods goods = new HZ.MBSM.Model.Goods();
        if (goods != null)
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                IsSalerId = SysFun.ToLong(Request.Form["txtIsSalerId"]);
                int rowCun = 0;
                Db db1 = new Db();
                System.Data.IDataReader dr;
                strSql = "select count(GOODSID) as cun from dbo.GOODS where   PRODUCTID='" + SysFun.ToTrim(hidProductId.Value) + "'  AND  FACTOR= '" + SysFun.ToTrim(SysFun.ToInt(txtFactor.Text)) + "' AND ACCOUNTID_SC='" + SysFun.ToTrim(IsSalerId) + "'";
                dr = db1.DataReader(strSql);
                if (dr.Read())
                {
                    rowCun = SysFun.ToInt(dr["cun"]);
                }
                dr.Close();
                dr.Dispose();
                db1.Close();
                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('该商品在数据库中已经存在！！！');</script>");

                }
                else
                {
                    AccountId_TJR = SysFun.ToLong(_Account.AccountId);

                    if (chkIsImport.Checked == true)
                    {
                        IsSalerId_dl = SysFun.ToLong(Request.Form["txtIsSalerId_dl"]);
                        goods.ImportCode = txtImportCode.Text.ToString();
                        goods.AccountId_Agent = IsSalerId_dl;// Convert.ToInt64(txtIsSalerId.Text.ToString());
                        if (txtImportRegistDate.Text != "")
                            goods.ImportRegistDate = Convert.ToDateTime(txtImportRegistDate.Text.ToString());
                        else
                            goods.ImportRegistDate = Convert.ToDateTime("1900-1-1");

                    }


                    goods.AccountId_SC = IsSalerId;// Convert.ToInt64(txtIsSalerId.Text.ToString());
                    goods.AccountId_TJR = AccountId_TJR;//添加人编码
                    goods.AccountName_TJR = _Account.AccountName;
                    goods.CheckStatus = "0";
                    goods.Factor = SysFun.ToInt(txtFactor.Text.ToString());
                    goods.GoodsInstruction = txtGoodsInstruction.Text.ToString();
                    goods.GoodsName = txtGoodsName.Text.ToString();

                    goods.IsSurveyReport = chkIsSurveyReport.Checked;
                    goods.IsImport = chkIsImport.Checked;
                    goods.LicenseNumber = txtLicenseNumber.Text.ToString();
                    if (txtLicenseNumberDate.Text != "")
                        goods.LicenseNumberDate = Convert.ToDateTime(txtLicenseNumberDate.Text.ToString());
                    else
                        goods.LicenseNumberDate = Convert.ToDateTime("1900-1-1");

                    goods.Material = txtMaterial.Text.ToString();
                    goods.Pack = labOutlookc.Text.ToString() + "×" + txtFactor.Text.ToString() + "/" + labUnit.Text.ToString() + "/"; goods.PackUnit = txtPackUnit.Text.ToString();
                    if (hidProductId.Value != "")
                        goods.ProductId = Convert.ToInt64(hidProductId.Value.ToString());
                    goods.QualityLevels = SysFun.ToTrim(rdoQualityLevels.SelectedValue);
                    if (txtRetailPrice.Text != "")
                        goods.RetailPrice = Convert.ToDouble(txtRetailPrice.Text.ToString());
                    if (txtSurveyReportDate.Text != "")
                        goods.SurveyReportDate = Convert.ToDateTime(txtSurveyReportDate.Text.ToString());
                    else
                        goods.SurveyReportDate = Convert.ToDateTime("1900-1-1");
                    goods.SurveyReportId = txtSurveyReportId.Text.ToString();
                    goods.memo4 = SysFun.ToTrim(txtMemo4.Text);
                    goods.ProductNameSM = txtProductNameSM.Text;
                    HZ.MBSM.BLL.Goods goodsOperator = new HZ.MBSM.BLL.Goods();
                    if (goodsOperator.Add(goods))
                    {
                        dr = null;
                        db1 = null;
                        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "插入", "goods", goods.GoodsId + goods.GoodsName + goods.GroupId + goods.ImportCode + goods.Licence + goods.LicenseNumberDate + goods.Material + goods.NewMedicine + goods.NewMedicineDate + goods.Pack + goods.PackUnit + goods.ProductId + goods.QualityLevels + goods.RetailPrice, "../Magage/goodsadd.aspx");
                        Response.Write("<script>alert('保存成功！！！');window.close();</script>");
                    }
                    else
                    {
                        dr = null;
                        db1 = null;
                        Response.Write("<script>alert('保存失败！！！');</script>");
                    }


                }
            }
            else
            {
                goods = null;
                Response.Write("<script language=JavaScript>alert('您无权添加商品！！！');</script>");
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("<script>window.close();</script>");
    }
}
